PART A
Consider a diet problem with two different foods (Food 1 and Food 2) and three different nutrients (Nutrient 1, Nutrient 2, and Nutrient 3). The amount of ounces of each nutrient per unit of food is given in table. The price of Food 1 is \$2. The price of Food 2 is \$3. The minimum requirements for the Nutrients 1, 2 and 3 are 30, 20, and 12 ounces, respectively. The optimization problem is to find the minimum cost diet that meets the minimum nutritional requirements.
Nutrient 1 | Nutrient 2 | Nutrient 3 | Price(\$) | |
---|---|---|---|---|
Food 1 | 4 | 6 | 1 | 2 |
Food 2 | 6 | 2 | 2 | 3 |
Requirements | 30 | 20 | 12 |
The decision variables are
The formulation is
$$
\left. \begin{array}{rrcl} \min & 2x+3y & &\\
\text {Subject to:} & & & \\
\text{Nutrient1} & 4x+6y & \ge & 30 \\
\text{Nutrient2} & 6x+2y & \ge & 20 \\
\text{Nutrient3} & x+2y & \ge & 12 \\
\text{Non negativity} & x,y & \ge & 0 \\
\end{array}
\right\}
$$
Solve the optimization problem in Julia.
In [8]:
using JuMP
#Define Model
MinCost = Model()
# Non-negative variables
@variable(MinCost, x >= 0)
@variable(MinCost, y >= 0)
# Daily requirement of each constraints
@constraint(MinCost, 4x + 6y >= 30)
@constraint(MinCost, 6x + 2y >= 20)
@constraint(MinCost, x + 2y >= 12)
# Minimize the cost
@objective(MinCost, Min, 2x + 3y)
# Solving the optimization problem
solve(MinCost)
# Determine the value each food needs to buy
println("Amount of food 1 to buy: ", getvalue(x))
println("Amount of food 2 to buy: ", getvalue(y))
# Determine the Minimum cost
println("Minimum amout of money spend: ", getobjectivevalue(MinCost))
In [9]:
print(getvalue(x)+getvalue(y))
PART B
Using fooddata.csv file, which contains randomly generated nutritional values for 10 different types of food.
We wish to minimize the number of calories consumed while intaking at least 50 fats, 300 carbohydrates, 60 proteins, and no more than 20 saturated fats.
Formulate a linear program, and determine the minimum number of calories consumed. The "read_csv" or "readtable" function in Julia will be helpful in reading the data set into memory.
In [22]:
using DataFrames
Nutrition = readtable("fooddata.csv")
Out[22]:
In [62]:
# Reading the food nutrients
FoodIndex = [parse(Int64,s) for s = Nutrition[2:11,1]]
Calories = [parse(Int64,s) for s = Nutrition[2:11,2]]
Fat = [parse(Int64,s) for s = Nutrition[2:11,3]]
SatFat = [parse(Int64,s) for s = Nutrition[2:11,4]]
Carb = [parse(Int64,s) for s = Nutrition[2:11,5]]
Protein = [parse(Int64,s) for s = Nutrition[2:11,6]]
Out[62]:
In [63]:
using JuMP
MinCal = Model()
# Non-negative food variables
@variable(MinCal, x[1:10] >= 0)
# Minimum of each nutrients
@constraint(MinCal, sum{x[i] * Fat[i], i=1:10} >= 60)
@constraint(MinCal, sum{x[i] * SatFat[i], i=1:10} <= 20)
@constraint(MinCal, sum{x[i] * Carb[i], i=1:10} >= 300)
@constraint(MinCal, sum{x[i] * Protein[i], i=1:10} >= 60)
# Minimize the Calories
@objective(MinCal, Min, sum{x[i] * Calories[i], i=1:10})
# Solving the model
solve(MinCal)
println("Optimal amount of calories: ", getobjectivevalue(MinCal))
In [67]:
# Format the output according to the problem.
using Formatting
fe = FormatExpr(" {1:04.2f}")
printfmtln(fe, getobjectivevalue(MinCal))
In [ ]:
In [ ]:
In [ ]: